import {Component, OnInit, ViewChild, ViewEncapsulation} from '@angular/core';
import {ActivatedRoute, Params, Router} from "@angular/router";
import {TrainService} from "@train/train.service";
import {ImageServeService} from "@shared/service/image/image-serve.service";
import {SeedInfiniteScroll} from "@shared/components/infinite-scroll/seed-infinite-scroll/seed-infinite-scroll.component";
import {TrainSubjectCommentListVO} from "@interface/index";
import {ToolsService} from "@shared/service/tools/tools.service";

@Component({
  selector: 'seed-class-remark',
  templateUrl: './class-remark.component.html',
  styleUrls: ['./class-remark.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class ParentClassRemarkComponent implements OnInit {
  @ViewChild(SeedInfiniteScroll) infiniteScroll;
  public lessons: Array<TrainSubjectCommentListVO> = [];
  public tabArray = [];
  public className: '';
  public teacherCommentInfo = {
    classId: 0,
    studentId: 0,
    commentState: 1,
    page: 1,
    rows: 10
  };
  public classId: number;
  public defaultIcon = '';
  public switchState = true;

  constructor(
    private activatedRoute: ActivatedRoute,
    private trainService: TrainService,
    private imgService: ImageServeService,
    public toolService: ToolsService,
    private router: Router
  ) {
    let lessonTabs = [{
      type: 'teacherComment',
      name: '教练点评'
    }, {
      type: 'teachPlan',
      name: '教学计划'
    }, {
      type: 'teacherInfo',
      name: '教练信息'
    }];
    this.activatedRoute.queryParams.switchMap((param: Params) => {
      this.className = param['className'];
      param['courseType'] === '2' && lessonTabs.splice(1, 1);
      this.tabArray = lessonTabs;
      return this.activatedRoute.params;
    }).subscribe((param: Params) => {
      this.teacherCommentInfo.classId = param['classId'];
      this.classId = param['classId'];
      this.teacherCommentInfo.studentId = param['studentId'];
      this.getLessonListForParent();
      this.defaultIcon = this.imgService.getMiddleArticleImgHolder();
    });
  }

  /**
   * @author guoqs1331
   * @time 2018/2/28
   * @description
   */
  switchCommentState(state: number): void{
    this.teacherCommentInfo.commentState = state;
    this.teacherCommentInfo.page = 1;
    this.switchState = true;
    this.infiniteScroll.enable(true);
    this.getLessonListForParent();
  }

  /**
   * @author guoqs1331
   * @time 2018/1/29
   * @description 查询课时列表（家长）
   */
  getLessonListForParent() {
    const {classId, studentId, commentState, page, rows} = this.teacherCommentInfo;
    const params = {
      classId: classId,
      studentId: studentId,
      commentState: commentState,
      page: page,
      rows: rows
    };
    this.trainService.getLessonListForParent(params).subscribe((res) => {
      if (res.result === '0' && res.data) {
        const result = res.data;
        const lessons = result.list;
        this.teacherCommentInfo.page++;
        this.infiniteScroll.complete();
        if(this.switchState){
          this.lessons = lessons;
        }else {
          this.lessons = [...this.lessons, ...lessons];
        }
        this.switchState = false;
        if (!result.hasNextPage) {
          this.infiniteScroll.enable(false);
        }
      }
    });
  }

  /**
   * @author guoqs1331
   * @time 2018/1/30
   * @description 查看点评
   */
  goLessonComment(lesson) {
    let nowTime = new Date().getTime();
    if (nowTime < lesson.beginDate) {
      this.toolService.showToast('未到上课时间，暂无点评');
      return;
    }
    if (lesson.state === 0 || lesson.state === 2 || lesson.state === 3) {
      this.toolService.showToast('该课时未销课，暂无点评');
      return;
    }
    let param = {
      scheduleId: lesson.scheduleId,
      studentId: this.teacherCommentInfo.studentId
    };
    this.trainService.updateRemarkReadState(param).subscribe();
    this.router.navigate(['/train/remark/parent/lesson'], {
      queryParams: {
        'studentId': this.teacherCommentInfo.studentId,
        'lessonId': lesson.scheduleId
      }
    });
  }

  ngOnInit() {
  }

}
